package org.example.test4;

import io.netty.handler.codec.http.HttpResponseStatus;
import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServer;

public class WebServer {
    public static final int PORT = 8080;

    public static void main(String[] args) {

        HttpServer server = HttpServer.create()
                .host("localhost")
                .port(PORT);

        server.route(routes -> {
            routes.get("/", (request, response) -> {
                // 处理 GET /hello 请求
                return response.sendString(Mono.just("Hello, World!"));
            });
            routes.get("hello", (request, response) -> {
                // 处理 GET /hello 请求
                return response.sendString(Mono.just("Hello, World!"));
            });

            routes.post("/user", (request, response) -> {
                // 处理 POST /user 请求
                // 获取请求体，处理用户创建逻辑等等
                return request.receive()
                        .aggregate()
                        .asString()
                        .flatMap(payload -> {
                            // 解析请求体中的用户信息并创建用户
                            // TODO: 处理创建用户的逻辑
                            // 返回创建的用户信息
//                            return (Mono<? extends Void>) response.status(HttpResponseStatus.CREATED)
//                                    .header("Content-Type", "text/plain")
//                                    .sendString(Mono.just("User created successfully"));
                            return (Mono<? extends Void>) response.status(HttpResponseStatus.CREATED)
                                    .header("Content-Type", "text/plain")
                                    .sendString(Mono.just("User created successfully"));
                        });
            });
        });
        System.out.println("start server port :" + PORT);

        server.bindNow().onDispose().block();
    }
}
